<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.pushType"
          clearable
          placeholder="消息推送类型"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        top="10px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-width="100px"
          inline
          style="height: 500px"
        >
          <el-col :span="12">
            <el-form-item label="模板标题">
              <el-input v-model="form.templateName" style="width: 220px;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消息推送类型">
              <el-select v-model="form.pushType" filterable placeholder="请选择" style="width: 220px;">
                <el-option
                  v-for="item in dict.messageType"
                  :key="item.id"
                  :label="item.dictValue"
                  :value="item.dictKey"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模板类型">
              <el-select v-model="form.templateType" filterable placeholder="请选择" style="width: 220px;">
                <el-option
                  v-for="item in dict.templateType"
                  :key="item.id"
                  :label="item.dictValue"
                  :value="item.dictKey"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="模板图标">
              <el-upload
                ref="my-upload"
                :on-success="handleSuccess"
                :on-error="handleError"
                :headers="headers"
                :action="imagesUploadApi"
                :data="type"
                :show-file-list="false"
              >
                <img
                  v-if="form.templateIcon"
                  :src="baseApi+form.templateIcon"
                  class="avatar"
                  width="150px"
                  height="150px"
                >
                <el-button v-else size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="模板内容">
              <el-input v-model="form.templateContent" :rows="20" type="textarea" style="width: 600px;" />
            </el-form-item>
          </el-col>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        stripe
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="templateName" label="模板标题"       />
        <el-table-column prop="templateIcon" label="模板图标"      >
          <template slot-scope="scope">
            <img :src="baseApi + scope.row.templateIcon" style="height: 50px">
          </template>
        </el-table-column>
        <el-table-column prop="templateContent" label="模板内容"       />
        <el-table-column prop="pushType" label="消息推送类型"      >
          <template slot-scope="scope">
            {{ dict.label.messageType[scope.row.pushType] }}
          </template>
        </el-table-column>
        <el-table-column prop="templateType" label="模板类型"      >
          <template slot-scope="scope">
            {{ dict.label.templateType[scope.row.templateType] }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="checkPer(['admin','messageTemplate:edit','messageTemplate:del'])"
          label="操作"
          width="150px"

        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudMessageTemplate from '@/api/msg/messageTemplate'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'

const defaultForm = {
  templateIcon: null,
  templateName: null,
  templateContent: null,
  pushType: null,
  templateType: null
}
export default {
  name: 'MessageTemplate',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['messageType', 'templateType'],
  cruds() {
    return CRUD({ title: '消息模版', url: 'messageTemplate/page', sort: 'id:desc', crudMethod: { ...crudMessageTemplate }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'messageTemplate:add'],
        edit: ['admin', 'messageTemplate:edit'],
        del: ['admin', 'messageTemplate:del']
      },
      rules: {},
      type: { type: 0 },
      imageUrl: '',
      headers: {
        'Authorization': getToken()
      },
      queryTypeOptions: [
        { key: 'pushType', display_name: '消息推送类型' }
      ]
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi', 'baseApi'])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    handleExceed(files, fileList) {
      this.$message.warning('当前限制选择 1张图片，请重新选择')
    },
    handleSuccess(res, file) {
      if (res.code === 0) {
        this.form.templateIcon = res.data.url
      } else {
        this.$message.error('图片上传失败,请重新上传')
      }
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    handleError(res) {
      this.$message.error('图片上传失败,请重新上传')
    }
  }
}
</script>

<style scoped>

</style>
